<template>
  <div class="box">
    <el-row>
      <el-col :span="24">
        <div class="header">
          <el-form
            ref="form"
            :model="form"
            label-width="100px"
            :rules="rules"
            class="flex-start"
          >
            <el-form-item label="所属网点" prop="networkLevel">
              <el-select
                v-model="form.networkLevel"
                placeholder="请选择所属网点"
                style="width: 275px"
              >
                <el-option
                  v-for="item in networkList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="时间" prop="time">
              <el-date-picker
                v-model="form.time"
                style="width: 650px"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                @change="mxDateChange"
              />
            </el-form-item>
            <el-form-item label="政策大类" prop="policyBigKind">
              <el-select
                v-model="form.policyBigKind"
                placeholder="请选择政策大类"
                style="width: 275px"
                clearable
                @change="subclass(form.policyBigKind)"
              >
                <el-option
                  v-for="(item, index) in CostBigKindlist"
                  :key="index"
                  :label="item.name"
                  :value="item.name"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="政策小类" prop="policySmallKind">
              <el-select
                v-model="form.policySmallKind"
                placeholder="请选择政策小类"
                style="width: 275px"
                clearable
              >
                <el-option
                  v-for="(item, index) in CostSmallKindlist"
                  :key="index"
                  :label="item.name"
                  :value="item.name"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button class="cx" @click="search">查询</el-button>
              <el-button @click="mxClear">重置</el-button>
              <el-button plain icon="el-icon-upload2" @click="exportData"
                >导出</el-button
              >
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <div>
          <el-table
            ref="multipleTable"
            v-loading="loading"
            max-height="600"
            :data="tableData"
            tooltip-effect="dark"
            style="width: 100%"
            border
            row-key="id"
            :tree-props="{ children: 'children' }"
            @selection-change="handleSelectionChange"
          >
            <el-table-column
              v-for="(t, n) in th"
              :key="n"
              :prop="t.fields"
              :label="t.translate"
              :fixed="n < 1"
              :min-width="flexColumnWidth(t.translate, t.fields, tableData)"
              align="center"
            >
              <template slot-scope="scope">
                <a
                  v-if="t.isPoint != null"
                  style="color: skyblue"
                  @click="searchDetail(scope.row, t)"
                  >{{ scope.row[t.fields] }}</a
                >
                <span v-else>{{ scope.row[t.fields] }}</span>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-col>
    </el-row>
    <div class="block" style="display: flex; justify-content: flex-end">
      <pagination
        :current-page="form.pageDate.pageNum"
        :page-size="form.pageDate.pageSize"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import {
  getOutletsList,
  getSubjectFirstList,
  // getSubjectList
  // subjectSearch,
  // addSubject,
} from "@/api/dashboard";
import {
  CostSmallKindApi,
  CostBigKindApi,
  CostSmallKindByBigApi,
} from "@/api/HqFinancePanel";
import { paymentsReportinit } from "@/api/apifile/Networkfinance";
import { exportDataHead } from "@/api/home";
export default {
  data() {
    return {
      CostSmallKindlist: [], // 小类
      CostBigKindlist: [], // 大类
      form: {
        networkLevel: 0,
        networkNameId: "",
        firstId: "",
        pageDate: {
          pageNum: 1,
          pageSize: 10,
        },
      },
      searchForm: {
        // contractArea: '',
        endTime: "",
        feeName: "",
        pageDate: {
          pageNum: 1,
          pageSize: 10,
        },
        settleNetworkNameId: "",
        startTime: "",
      },
      loading: false,
      formLabelWidth: "120px",
      addForm: {
        networkNameId: "",
        firstId: "",
        secondName: "",
      },
      // 一级网点
      SubjectFirstList: [],
      // 网点列表
      networkList: [
        { value: 0, label: "网点" },
        { value: 1, label: "承包区" },
        { value: 2, label: "客户" },
      ],
      pages: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      dialogFormVisible: false,
      currentPage4: 4,
      tableData: [],
      rules: {
        networkLevel: [
          { required: true, message: "请选择网点名称", trigger: "blur" },
        ],
        time: [{ required: true, message: "请选择日期", trigger: "blur" }],
      },
      th: [],
    };
  },
  // created() {
  //   this.getList();
  //   this.getOutLet();
  //   this.getSubjectFirst();
  // },
  created() {
    this.getCostBigKind();
    this.getCostSmallKind();
    // -----------------------
    // this.getList();
    // this.getOutLet();
    // this.getSubjectFirst();
  },
  methods: {
    searchDetail(item, type) {
      // this.searchForm.contractArea = this.networkList.filter(el => el.value === this.form.networkLevel)[0].label
      this.searchForm.settleNetworkNameId = item.networkName;
      this.searchForm.feeName = item.feeName;
      this.searchForm.policySmallKind = item.feeSmallKind;
      this.$store.state.app.zbszhztab = this.searchForm;
      this.$router.push({
        path: "paymentsReportDetail",
      });
    },
    subclass(name) {
      this.form.policySmallKind = "";
      if (name === "") {
        this.getCostSmallKind();
      } else {
        this.getCostSmallKindByParentName(name);
      }
    },
    getCostBigKind() {
      CostBigKindApi().then((res) => {
        if (res.code === 200) {
          this.CostBigKindlist = res.data;
        }
      });
    },
    getCostSmallKind() {
      CostSmallKindApi().then((res) => {
        if (res.code === 200) {
          this.CostSmallKindlist = res.data;
        }
      });
    },
    getCostSmallKindByParentName(name) {
      CostSmallKindByBigApi(name).then((res) => {
        if (res.code === 200) {
          this.CostSmallKindlist = res.data;
        }
      });
    },
    exportData() {
      // console.log('导出')
      this.$refs.form.validate((res) => {
        if (res) {
          // this.init({ ...this.form, pageDate: this.pages })
          this.$notify.info({
            title: "开始准备下载...",
          });
          exportDataHead(this.form).then((res) => {
            const url = window.URL.createObjectURL(new Blob([res]), {
              type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
            });
            const link = document.createElement("a");
            link.href = url;
            link.setAttribute("download", "总部收支汇总报表.xlsx"); // 设置下载的文件名
            document.body.appendChild(link);
            link.click();
            link.parentNode.removeChild(link);
            this.$notify({
              title: "下载成功",
              type: "success",
            });
          });
        }
      });
    },
    handleSizeChange(val) {
      this.form.pageDate.pageSize = val;
      this.init(this.form);
    },
    handleCurrentChange(val) {
      this.form.pageDate.pageNum = val;
      this.init(this.form);
    },
    mxClear() {
      this.$refs.form.resetFields();
      this.form.startTime = "";
      this.form.endTime = "";
    },
    search() {
      this.$refs.form.validate((res) => {
        if (res) {
          this.init(this.form);
        }
      });
    },
    init(obj) {
      this.loading = true;
      paymentsReportinit(obj).then((res) => {
        this.loading = false;
        // console.log('查询', res)
        if (res.code === 200) {
          this.tableData = res.data.list.data;
          this.total = res.data.list.total;
          this.th = res.data.fields;
        } else {
          this.tableData = [];
          this.total = 0;
        }
      });
    },
    mxDateChange(value) {
      // // console.log('日期', value)
      if (value != null) {
        this.searchForm.startTime = value[0];
        this.searchForm.endTime = value[1];
        this.form.startTime = value[0];
        this.form.endTime = value[1];
      }
    },
    // 获取网点列表
    getOutLet() {
      getOutletsList().then((res) => {
        if (res.code === 200) {
          this.networkList = res.data;
        }
      });
    },
    // 获取一级网点下拉框
    getSubjectFirst() {
      getSubjectFirstList().then((res) => {
        if (res.code === 200) {
          this.SubjectFirstList = res.data;
        }
      });
    },
    // 打开弹窗
    onSubmit() {
      this.dialogFormVisible = true;
    },

    // 重置
    clear() {
      this.form = {};
      this.getList();
    },

    handleSelectionChange(val) {
      // this.multipleSelection = val;
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  padding: 20px;
}

.flex {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-wrap: wrap;
}

.flex-start {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-wrap: wrap;
}

.flex-bettom {
  display: flex;
  justify-content: space-between;
}

.header {
  padding-bottom: 20px;
  border-bottom: 1px solid #ccc;
}

.el-row {
  margin-bottom: 20px;

  &:last-child {
    margin-bottom: 0;
  }
}

.el-col {
  border-radius: 4px;
}

.bg-purple-dark {
  background: #99a9bf;
}

.bg-purple {
  background: #d3dce6;
}

.bg-purple-light {
  background: #e5e9f2;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
.cx {
  background: #2087d0;
  border-radius: 4px;
  color: #fff;
}
</style>
